function main() {
    //程序入口
    require(["pluginAttach", "knockout"], function (pluginAttach, ko) {
        var Product = function () {
            var self = this;
            self.name = ko.observable("牛奶").extend({
                minLength: 2,
                maxLength: {
                    params: 30,
                    message: "名称最大长度为30个字符",
                },
                required: {
                    params: true,
                    message: "请输入名称",
                },
            });

            self.price = ko.observable(5).extend({
                required: {
                    params: true,
                    message: "请输入价格",
                },
                min: {
                    params: 1,
                    message: "请输入大于1的整数",
                },
                max: 100,
            });

            self.categoryId = ko.observable("食品A01").extend({
                required: true,
                pattern: {
                    message: "只能由字母、数字、汉字组成",
                    params: "[\d|\w|\u4E00-\u9FA5]+",
                },
            });

            self.email = ko.observable("123@example.com").extend({
                required: {
                    params: true,
                    message: "请输入你的邮箱地址",
                },
                email: {
                    params: true,
                    message: "邮箱地址格式不正确",
                },
            });
        };
        Product.prototype.handleClickCreate = function (data, event) {
            var self = this;
            self.createProduct();
        };
        Product.prototype.createProduct = function () {
            var self = this;
            self.errors = ko.validation.group(self);
            let errors = self.errors();
            if (errors.length === 0) {
                alert("创建成功");
            } else {
                console.log(errors);
                self.errors.showAllMessages();
            }
        };
        //指定验证信息为中文
        ko.validation.locale("zh-CN");
        var vm = new Product();
        ko.applyBindings(vm, document.getElementById("root"));
    });
}
require(["../scripts/require.config"], main);
